package com.itbaima.csmall.stock.webapi.quartz.rabbit;

import com.itbaima.csmall.commons.pojo.stock.entity.Stock;
import com.itbaima.csmall.stock.webapi.quartz.config.RabbitMQConfig;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.math.RandomUtils;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * <p>RabbitMQJob</p>
 *
 * @author Chen
 * @since 2023/3/8 16:24:24
 */
@Slf4j
public class RabbitMQJob implements Job {

    @Autowired
    private RabbitTemplate rabbitTemplate;
    static int i = 1;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        Stock stock = new Stock();
        stock.setId(i++);
        stock.setCommodityCode("PC100");
        stock.setReduceCount(10 + RandomUtils.nextInt(90));
        rabbitTemplate.convertAndSend(RabbitMQConfig.STOCK_EX, RabbitMQConfig.STOCK_ROUTE, stock);
        log.info("消息发送完成：{}", stock);
    }

}
